import unittest
from alg import Solution
from gen import get_sample_cases, generate_test_case


class TestLetterCombinations(unittest.TestCase):
    def test_sample_cases(self):
        """测试示例用例"""
        sol = Solution()
        sample_cases = get_sample_cases()

        for i, (digits, expected) in enumerate(sample_cases, 1):
            print(f"示例 {i}：")
            result = sol.letterCombinations(digits)
            print(f"输入：digits = \"{digits}\"")
            print(f"输出：{result}")
            self.assertEqual(sorted(result), sorted(expected))
            print()  # 空行分隔每个示例

    def test_random_cases(self):
        """测试随机生成的用例"""
        sol = Solution()
        for _ in range(5):
            digits = generate_test_case()
            print(f"随机测试：")
            result = sol.letterCombinations(digits)
            print(f"输入：digits = \"{digits}\"")
            print(f"输出：{result}")
            expected = self.brute_force_letter_combinations(digits)
            self.assertEqual(sorted(result), sorted(expected))
            print()  # 空行分隔每个测试

    def brute_force_letter_combinations(self, digits: str) -> list[str]:
        """暴力解法用于验证结果"""
        if not digits:
            return []

        digit_map = {
            "2": "abc", "3": "def", "4": "ghi",
            "5": "jkl", "6": "mno", "7": "pqrs",
            "8": "tuv", "9": "wxyz"
        }

        from itertools import product
        letters_list = [digit_map[digit] for digit in digits]
        combinations = product(*letters_list)
        return [''.join(combo) for combo in combinations]


if __name__ == '__main__':
    unittest.main()    